package com.ruoyi.framework.web.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.service.IPayCoinUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqGameHome;
import com.ruoyi.wq.domain.WqUserGame;
import com.ruoyi.wq.domain.vo.WqGameHomeVO;
import com.ruoyi.wq.domain.vo.WqUserGameVO;
import com.ruoyi.wq.service.IWqGameHomeService;
import com.ruoyi.wq.service.IWqUserGameService;
import com.ruoyi.wx.domain.WxMpMsgForm;
import com.ruoyi.wx.service.WxMsgService;

import cn.hutool.core.date.DateUtil;

@Component
public class UserGameService {
	
	@Autowired
	private IWqUserGameService wqUserGameService;
	
	@Autowired
	private IWqGameHomeService wqGameHomeService;
	
	@Autowired
    private IPayCoinUserService payCoinUserService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
    private WxMsgService wxMsgService;

	public void removeHome(WqUserGameVO wqUserGame) {
		Long userId = SecurityUtils.getUserId();
		QueryWrapper<WqUserGame> userGameQuery = new QueryWrapper<>();
		WqUserGame entity = new WqUserGame();
		entity.setUserId(wqUserGame.getUserId());
		entity.setGameHomeId(wqUserGame.getGameHomeId());
		entity.setStatus("0");
		userGameQuery.setEntity(entity);
		WqUserGame userGame = wqUserGameService.getOne(userGameQuery);
		if(userGame != null){
			WqGameHome gameHome = wqGameHomeService.getById(wqUserGame.getGameHomeId());
			if(gameHome.getUserId().intValue() == userId.intValue()){
				if(gameHome.getUserId().intValue() == wqUserGame.getUserId().intValue()){
					throw new ServiceException("不能移出房主");
				}
				WqGameHomeVO gh = new WqGameHomeVO();
				gh.setId(gameHome.getId());
				gh.setGame(wqUserGame.getRole());
				wqGameHomeService.removeHome(gh);
				gh.setStatus("0");
				wqGameHomeService.updateById(gh);
			}else{
				throw new ServiceException("你不是房主");
			}
			
			userGame.setGameResult(wqUserGame.getGameResult());
			userGame.setStatus("1");
			wqUserGameService.updateById(userGame);
			
			// 返还趣币
			payCoinUserService.updateCoinUserBack(wqUserGame.getUserId(), gameHome.getId(), "移出队伍返还");
			
			// 发送通知
			SysUser su = sysUserService.getById(wqUserGame.getUserId());
			if(StringUtils.isNotBlank(su.getAppId())){
            	if(StringUtils.isNotBlank(su.getWxOpenId())){
            		WxMpMsgForm msgForm = new WxMpMsgForm();
            		msgForm.setWxId(su.getWxId());
            		msgForm.setAppId(su.getAppId());
            		msgForm.setOpenId(su.getWxOpenId());
            		Map<String,String> dataMap = new HashMap<>();
            		dataMap.put("first", "您好，您收到一个队伍通知");
            		dataMap.put("keyword1", su.getNickName());
            		dataMap.put("keyword2", DateUtil.now());
            		dataMap.put("keyword3", "您被移出队伍，趣币已返还");
            		dataMap.put("remark", "本条信息是站内信通知，祝您游戏愉快！点击查看详情");
            		msgForm.setDataMap(dataMap);
            		msgForm.setPage("pages/index/game");
            		wxMsgService.sendTemplateMsg(msgForm);
            	}
            }
		}
	}
	
}
